import {
  getPage
} from '../../utils/config';
import api from '../../utils/api';
import {
  http,
  $empty,
  $toast,
  storage,
  Validate
} from '../../utils/utils';
import wxApi from '../../utils/wxApi';
import uploadImage from '../../utils/aliyunoss/uploadAliyun';
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    content: '',
    fileList: [],
    videoList: []
  },
  messageInput(e) {
    this.setData({
      messageInputError: '',
      content: e.detail,
    });
  },
  deleteimg(event) {
    var index = event.detail.index;
    var fileList = this.data.fileList;
    fileList.splice(index, 1);
    this.setData({
      fileList: fileList
    });
  },
  deletevideo(event) {
    var index = event.detail.index;
    var videoList = this.data.videoList;
    videoList.splice(index, 1);
    this.setData({
      videoList: videoList
    });
  },
  afterRead(event) {
    const {
      file
    } = event.detail;
    var that = this;
    var fileList = that.data.fileList;

    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    for (var i = 0; i < file.length; i++) {
      var item = file[i];
      uploadImage({
        filePath: item.url,
        dir: "anli",
        success(res) {
          // 上传完成需要更新 fileList

          fileList.push({
            url: api.imageCdn + res,
            picurl: res
          });
          that.setData({
            fileList: fileList
          });
        },
        fail(res) {
          $toast(`图片上传失败${JSON.stringify(res)}`);
        }
      });
    }

  },
  videoafterRead(event) {
    const {
      file
    } = event.detail;
    var that = this;
    console.log(file)
    var videoList = that.data.videoList;
    // 当设置 mutiple 为 true 时, file 为数组格式，否则为对象格式
    for (var i = 0; i < file.length; i++) {
      var item = file[i];
      uploadImage({
        filePath: item.url,
        dir: "anli",
        success(res) {
          // 上传完成需要更新 fileList
          console.log(res);
          videoList.push({
            url: api.imageCdn + res,
            picurl: res
          });
          that.setData({
            videoList: videoList
          });
        },
        fail(res) {
          $toast(`图片上传失败${JSON.stringify(res)}`);
        }
      });
    }

    console.log(videoList);
  },
  /**
   * 提交保存
   */
  anliForm(e) {
    // Validate.check(this, this.expressValidate, e.detail.value, (res) => {
    //   if (res) {

    //   }
    // });
    const data = this.data;
    console.log(data);
    const fileList = data.fileList;
    const videoList = data.videoList;
    const {
      shopid,
      userId,
      content
    } = this.data;
    const params = {
      action: 'setshopanli',
      content: content,
      shopid: shopid,
      userid: userId,
      fileList: JSON.stringify(fileList),
      videoList: JSON.stringify(videoList),
    }
    http.post(api.default, {
      data: params
    }).then((res) => {
      if (res.data.code == 0) {
        $toast('保存成功', {
          icon: 'success'
        });
        const pages = getCurrentPages()[getCurrentPages().length - 2];

        wx.navigateBack({
          delta: 1,
          success() {
            if (pages !== undefined || pages !== null) {
              pages.data.pageChange = 1;
              pages.onLoad();
            }
          }
        });
      } else {
        $toast(res.data.errmsg, {
          icon: 'fail'
        });
      }
    });
  },


  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {

    app.getLogin().then((res) => {
      this.setData({
        userInfo: res,
        logged: res.code,
        userId: res.data.user.user_id,
        shopid: res.data.shop.ShopId
      });
      //店铺案例
      http.post(api.default, {
        data: {
          action: 'GetShopanli',
          shopid: res.data.shop.ShopId
        }
      }, false).then((res) => {
        const {
          data
        } = res.data;
        this.setData({
          fileList: data.list,
          videoList: data.videolist,
          content: data.des
        });
      });
    })


    //获取验证规则
    // Validate.rules('#message', (res) => {
    //   this.expressValidate = res;
    // });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  }
})